//
//  WFCUCardCell.m
//  WFChat UIKit
//
//  Created by WF Chat on 2017/9/1.
//  Copyright © 2017年 WildFireChat. All rights reserved.
//

#import "WFCUCardCell.h"
#import <WFChatClient/WFCChatClient.h>
#import "WFCUUtilities.h"
#import "UILabel+YBAttributeTextTapAction.h"
#import <SDWebImage/SDWebImage.h>
#import "WFCUImage.h"
#import "YGExtendChain.h"
#import "SDAutoLayout.h"
#import "UIView+Toast.h"

#define TEXT_TOP_PADDING 6
#define TEXT_BUTTOM_PADDING 6
#define TEXT_LEFT_PADDING 8
#define TEXT_RIGHT_PADDING 8


#define TEXT_LABEL_TOP_PADDING TEXT_TOP_PADDING + 4
#define TEXT_LABEL_BUTTOM_PADDING TEXT_BUTTOM_PADDING + 4
#define TEXT_LABEL_LEFT_PADDING 30
#define TEXT_LABEL_RIGHT_PADDING 30

@interface WFCUCardCell ()
@property (nonatomic, strong)UIImageView *cardPortrait;
@property (nonatomic, strong)UILabel *cardDisplayName;
@property (nonatomic, strong)UILabel *cardName;
@property (nonatomic, strong)UIView *cardSeparateLine;
@property (nonatomic, strong)UILabel *cardHint;
@end

@implementation WFCUCardCell

+ (CGSize)sizeForClientArea:(WFCUMessageModel *)msgModel withViewWidth:(CGFloat)width {
    return CGSizeMake(width, 100);
}

- (void)setModel:(WFCUMessageModel *)model {
    [super setModel:model];
    
    WFCCCardMessageContent *content = (WFCCCardMessageContent *)model.message.content;
    
    self.cardDisplayName.text = content.displayName;
    self.cardName.text = content.name;
    [self.cardPortrait sd_setImageWithURL:[NSURL URLWithString:content.portrait] placeholderImage:[WFCUImage imageNamed:@"PersonalChat"]];
    
    [self cardSeparateLine];
    [self cardHint];
}

- (UIImageView *)cardPortrait {
    if (!_cardPortrait) {
        _cardPortrait = [[UIImageView alloc] initWithFrame:CGRectMake(8, 8, 56, 56)];
        [self.contentArea addSubview:_cardPortrait];
    }
    return _cardPortrait;
}

- (UILabel *)cardDisplayName {
    if (!_cardDisplayName) {
        CGRect bounds = self.contentArea.bounds;
        _cardDisplayName = [[UILabel alloc] initWithFrame:CGRectMake(72, 10, bounds.size.width - 72 - 8, 24)];
        [self.contentArea addSubview:_cardDisplayName];
    }
    return _cardDisplayName;
}

- (UILabel *)cardName {
    if (!_cardName) {
        CGRect bounds = self.contentArea.bounds;
        _cardName = [[UILabel alloc] initWithFrame:CGRectMake(72, 40, bounds.size.width - 72 - 8, 18)];
        _cardName.font = [UIFont systemFontOfSize:14];
        _cardName.textColor = [UIColor grayColor];
        [self.contentArea addSubview:_cardName];
    }
    return _cardName;
}

- (UIView *)cardSeparateLine {
    if (!_cardSeparateLine) {
        CGRect bounds = self.contentArea.bounds;
        _cardSeparateLine = [[UIView alloc] initWithFrame:CGRectMake(8, 78, bounds.size.width - 8 - 8, 1)];
        _cardSeparateLine.backgroundColor = [UIColor grayColor];
        [self.contentArea addSubview:_cardSeparateLine];
    }
    return _cardSeparateLine;
}

- (UILabel *)cardHint {
    if (!_cardHint) {
        _cardHint = [[UILabel alloc] initWithFrame:CGRectMake(8, 84, 80, 12)];
        _cardHint.font = [UIFont systemFontOfSize:10];
        _cardHint.text = WFCString(@"PersonalCard");
        _cardHint.textColor = [UIColor grayColor];
        [self.contentArea addSubview:_cardHint];
    }
    return _cardHint;
}
@end




@interface WFCJoinGroupCell ()
@property (nonatomic, strong)UIImageView *cardPortrait;
@property (nonatomic, strong)UILabel *cardDisplayName;
@property (nonatomic, strong)UILabel *cardName;
@property (nonatomic, strong)UIView *cardSeparateLine;
@property (nonatomic, strong)UILabel *cardHint;
@property (nonatomic, strong)UIButton *btn1,*btn2;

@end

@implementation WFCJoinGroupCell

+ (CGSize)sizeForClientArea:(WFCUMessageModel *)msgModel withViewWidth:(CGFloat)width {
    return CGSizeMake(width,   115 );
//    return CGSizeMake(width, msgModel.isMyselfManager == YES ? 150 : 115 );

}

- (void)setModel:(WFCUMessageModel *)model {
    [super setModel:model];
    
    WFCJoinGroupMessageContent *content = (WFCJoinGroupMessageContent *)model.message.content;
    
    self.cardDisplayName.text = content.displayName;
//    self.cardName.text = content.name;
    [self.cardPortrait sd_setImageWithURL:[NSURL URLWithString:content.portrait] placeholderImage:[WFCUImage imageNamed:@"PersonalChat"]];
    
    [self cardSeparateLine];
    [self cardHint];
//    [self btn1];
 
    
}

- (UIImageView *)cardPortrait {
    if (!_cardPortrait) {
        _cardPortrait = [[UIImageView alloc] initWithFrame:CGRectMake(8, CGRectGetMaxY(self.cardSeparateLine.frame)  + 8, 56, 56)];
        [self.contentArea addSubview:_cardPortrait];
        _cardPortrait.layer.masksToBounds = YES;
        _cardPortrait.layer.cornerRadius = 10;
    }
    return _cardPortrait;
}

- (UILabel *)cardDisplayName {
    if (!_cardDisplayName) {
        CGRect bounds = self.contentArea.bounds;
        _cardDisplayName = [[UILabel alloc] initWithFrame:CGRectMake(72,CGRectGetMaxY(self.cardSeparateLine.frame)  + 8 + 10, bounds.size.width - 72 - 8, 24)];
        [self.contentArea addSubview:_cardDisplayName];
    }
    return _cardDisplayName;
}

- (UILabel *)cardName {
    if (!_cardName) {
        CGRect bounds = self.contentArea.bounds;
        _cardName = [[UILabel alloc] initWithFrame:CGRectMake(72, 40, bounds.size.width - 72 - 8, 18)];
        _cardName.font = [UIFont systemFontOfSize:14];
        _cardName.textColor = [UIColor grayColor];
        [self.contentArea addSubview:_cardName];
    }
    return _cardName;
}

- (UIView *)cardSeparateLine {
    if (!_cardSeparateLine) {
        CGRect bounds = self.contentArea.bounds;
        _cardSeparateLine = [[UIView alloc] initWithFrame:CGRectMake(8, CGRectGetMaxY(self.cardHint.frame) + 8 , bounds.size.width - 8 - 8, 0.5)];
        _cardSeparateLine.backgroundColor = [UIColor grayColor];
        [self.contentArea addSubview:_cardSeparateLine];
    }
    return _cardSeparateLine;
}

- (UILabel *)cardHint {
    if (!_cardHint) {
        _cardHint = [[UILabel alloc] initWithFrame:CGRectMake(8, 8, 180, 20)];
        _cardHint.font = [UIFont systemFontOfSize:15];
        _cardHint.text = WFCString(@"邀请加入群聊");
        _cardHint.textColor = [UIColor grayColor];
        [self.contentArea addSubview:_cardHint];
    }
    return _cardHint;
}

- (UIButton *)btn1{
    if (!_btn1) {
         WEAKSELF_ZM
        _btn1 = [[UIButton alloc] initWithFrame:CGRectMake(8 + 20, CGRectGetMaxY(self.cardPortrait.frame) + 8, 80, 40)];
        
        [self.contentArea addSubview:_btn1];
        _btn1.sd_layout.centerXEqualToView(self.contentArea).topSpaceToView(self.cardPortrait, 12).heightIs(40).widthIs(80);
        _btn1.sh_chain_extend.title(@"同意申请").titleColor([UIColor colorWithHexExtendString:@"#016EFF"]).font_size(15);
        [_btn1
         jk_addTapActionWithBlock:^(UIGestureRecognizer *gestureRecoginzer) {
            WFCJoinGroupMessageContent *content = (WFCJoinGroupMessageContent *)weakSelf.model.message.content;

            NSString *target = weakSelf.model.message.conversation.target;
            [[WFCCIMService sharedWFCIMService] addMembers:@[content.targetId] toGroup:target memberExtra:nil notifyLines:@[@(0)] notifyContent:nil success:^{
              [[WFCCIMService sharedWFCIMService] getGroupMembers:target forceUpdate:YES];
                
            } error:^(int error_code) {
                if (error_code == ERROR_CODE_GROUP_EXCEED_MAX_MEMBER_COUNT) {
                    [Zm_getCurrentVC().view makeToast:WFCString(@"ExceedGroupMaxMemberCount") duration:1 position:CSToastPositionCenter];
                } else {
                    [Zm_getCurrentVC().view makeToast:WFCString(@"NetworkError") duration:1 position:CSToastPositionCenter];
                }
            }];
            
            
        }];
     
//        _btn2 = [[UIButton alloc] initWithFrame:CGRectMake(8 + 20 + CGRectGetMaxX(self.btn1.frame) , CGRectGetMaxY(self.cardPortrait.frame) + 12, 60, 40)];
//        
//        [self.contentArea addSubview:_btn2];
//        _btn2.sh_chain_extend.title(@"拒绝").titleColorRed().font_size(15);
 
        
    }
    return _btn1;
}


@end
